import re
import sys
import os

# 获取当前脚本所在目录的父目录
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# 将父目录添加到sys.path中
sys.path.append(os.path.join(parent_dir, 'my_package'))
from my_package.china_address_mapping import province_dict, address_dict

# 正则表达式模式
id_pattern = re.compile(r'^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[0-9Xx]$')

while True:
    id_number = input("请输入身份证号：")
    if id_pattern.match(id_number):
        # 获取省份编码
        province_code = id_number[:2]
        province = province_dict.get(province_code, '未知省份')

        # 获取详细地址
        address_code = id_number[:6]
        address = address_dict.get(address_code, '未知地区')

        # 获取出生年月日
        birth_year = id_number[6:10]
        birth_month = id_number[10:12]
        birth_day = id_number[12:14]

        print(f"身份证号合规，所在地址：{province}{address}，出生年月日：{birth_year}年{birth_month}月{birth_day}日")
        break
    else:
        print("输入的身份证号不合规，请重新输入。")
